package Servlet; /**
 * 项目名称：deleteServlet.java
 * 文件名称：deleteServlet.java
 * 类的描述：无
 * 创建时间：2025-07-18 15:01:20
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */

import java.io.*;

import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "deleteServlet", value = "/api/delete")
public class deleteServlet extends HttpServlet {
    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int myid= (int) req.getSession().getAttribute("id");

        if(id!=myid) {
            String sql = "delete from user where id=?";
            GokJDBCUtil2.update(sql, id);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"200\",\"msg\":\"删除成功\"}");
        }else{
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"400\",\"msg\":\"不能删除自己\"}");
        }
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("application/json; charset=UTF-8");
        String method = req.getMethod();
        if (method.equals("GET")) {
            this.doGet(req, res);
        }else {
            PrintWriter writer = res.getWriter();
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/json");
            writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}